package com.rcl.mvvmdemo.player

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import com.rcl.mvvmdemo.R
import com.rcl.mvvmdemo.base.BaseActivity
import kotlinx.android.synthetic.main.activity_flow_player.*
import kotlinx.android.synthetic.main.activity_player.*

class FlowPlayerControllerActivity : BaseActivity() {

    private val playerPresenter by lazy {
        PlayerPresenter.instance
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_flow_player)
        initListener();
        initDataListener()
    }

    override fun onDestroy() {
        super.onDestroy()
        LivePlayerState.instance.removeObserver(livePlayerStateObserver)
    }

    private val livePlayerStateObserver by lazy {
        LivePlayerStateObserver()
    }

    inner class LivePlayerStateObserver : Observer<PlayerPresenter.PlayState> {
        override fun onChanged(t: PlayerPresenter.PlayState?) {
            Log.i("aaaaa", "悬浮按钮页面...live data -->当前状态-->$t")
        }
    }

    /**
     * 对数据进行监听
     */
    private fun initDataListener() {
        LivePlayerState.instance.observeForever(livePlayerStateObserver)
        playerPresenter.currentPlayState.addListener(this) {
            playOrePauseBtn.text = if (it === PlayerPresenter.PlayState.PLAYING) {
                "暂停"
            } else {
                "播放"
            }
        }
    }

    private fun initListener() {
        playOrePauseBtn.setOnClickListener {
            playerPresenter.doPlayOrPause()
        }
    }
}